package no.kolstad.movies.model;
/**
 *  Copyright [2008] Christopher Kolstad
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License. 
 */
import java.io.Serializable;
import javax.persistence.*;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import org.springframework.security.GrantedAuthority;

import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@NamedQuery(name = "role.byName", query = "FROM Role r WHERE r.rolename = :rolename")
public class Role extends BaseModelObject implements Serializable,
    GrantedAuthority {
  private Long id;
  private String rolename;
  private String displayname;
  private Long version;

  @Id
  @GeneratedValue
  public Long getId() {
    return id;
  }

  private void setId(Long id) {
    this.id = id;
  }

  @Version
  public Long getVersion() {
    return version;
  }

  private void setVersion(Long version) {
    this.version = version;
  }

  @Column(unique = true)
  public String getRolename() {
    return rolename;
  }

  public void setRolename(String rolename) {
    this.rolename = rolename;
  }

  public String getDisplayname() {
    return displayname;
  }

  public void setDisplayname(String displayname) {
    this.displayname = displayname;
  }

  public String toString() {
    return new ToStringBuilder(this).append("id", id)
        .append("version", version).append("rolename", rolename).toString();
  }

  // ACEGI methods (GrantedAuthority)

  @Transient
  public String getAuthority() {
    return getRolename();
  }

    @Override
    public int compareTo(Object o) {
        if(o.getClass().equals(this.getClass())) {
            Role r2 = (Role) o;
            return this.getId().compareTo(r2.getId());
        }
        else {
            throw new UnsupportedOperationException("Wrong type of Object for comparison");
        }
    }

}
